# -*- coding: utf-8 -*-

# __date:       2021/7/1
# __author:     Yang Chao
# __function:   String to Integer (atoi) 字符串转换整数

class Solution:

    def myAtoi(self, s: str) -> int:
        s = s.lstrip()
        res = ""
        sign = 1
        flag = True
        if not s:
            return 0
        if s[0] == '-' or s[0] == '+':
            sign = -1 if s[0] == '-' else 1
            s = s[1:]
        for n in s:
            if n == '0':
                if flag:
                    continue
                res += n
            elif n.isdigit():
                flag = False
                res += n
            else:
                break
        if res:
            res = int(res)
        else:
            return 0
        res *= sign
        if res < 0: return max(res, -2**31)
        else: return min(res, 2 ** 31 - 1)


if __name__ == '__main__':
    s = Solution()
    res = s.myAtoi("-42")
    print(res)